qui(
    function (require, exports, module) {
        StreamSearch.Grid = function (options) {
            this.dao = StreamSearch.Dao;
            this.config = StreamSearch.Config;
            this.options = options || {};
        };

        Com.inherits(StreamSearch.Grid, Ancestor.Grid).extend({
            init: function () {
                var self = this;
                var $container = self.options.$container;
                self.$grid = $container.find('table.grid');
                self.bindEvent();
            },


            initConfigOfType: function (options) {

                Ancestor.Dao.getType({
                    type: options.sn
                }, function (json) {
                    if (json.code == 'A00000') {
                        var config = Stream.Config[options.sk];
                        if (options.sn == 'RtmpLocation') {
                            config[''] = '';
                        }

                        $.each(json.data, function () {
                            if (options.sn == 'StreamType' || options.sn == 'RtmpLocation')
                                config[this.val] = this.descr;
                            else
                                config[this.key] = this.descr;
                        });

                    } else {
                        console.log('fail!');
                    }

                    if (typeof(options.requestLoaded) == 'function') {
                        options.requestLoaded();
                    }

                });
            },

            run: function () {
                var self = this;
                this.ajaxBasicRequestQueue.pushData({
                    func: function (ajaxBasicRequest) {
                        self.initConfigOfType({
                            'sn': 'StreamType',
                            'sk': 'TYPE',
                            requestLoaded: ajaxBasicRequest
                        });
                    }
                });

                this.ajaxBasicRequestQueue.pushData({
                    func: function (ajaxBasicRequest) {
                        self.initConfigOfType({
                            'sn': 'StreamStatus',
                            'sk': 'STATUS',
                            requestLoaded: ajaxBasicRequest
                        });
                    }
                });

                this.ajaxBasicRequestManage.init(this, function (obj) {
                    obj.start();
                });
            },

            start: function (options, callback) {
                var self = this,
                    gridConfig = self.getGridConfig();

                self.init();

                self.echoQuery();
                options = $.extend(true, {
                    // width: 1500,
                    shrinkToFit: false
                    // autowidth: true
                }, gridConfig, options);
                self.initGrid.call(this, options, callback);
            },

            initGrid: function (options, callback) {
                var self = this;

                var gridConfig = self.getGridConfig();

                var $gridContainer = self.options.$container;

                var $pager = $gridContainer.find('div.pager');
                var $grid = $gridContainer.find('table.grid');

                options = $.extend(true, {
                    pager: $pager,
                    mtype: 'post',

                    postData: {
                        streamName: ""
                    },
                    multiselect: true,
                    multiselectWidth: 30

                }, gridConfig, options);


                //$grid.jqGrid('scroll', {});
                $grid.jqGrid(options);

                var params = $grid.jqGrid("getGridParam");
                params.rowNum = 20;
                params.pages = 1;

            },

            query: function () {
                var self = this;
                var $container = self.options.$container;
                var $grid = self.$grid;
                self.overridePostData($grid);
                var params = self.getParam();

                //params.fatherId = 10117;

                //StreamSearch.Config.TABLE.jsonReader = {
                //    root: "data.result",
                //    page: "data.pageNo",
                //    total: "data.totalCount/data.pageSize",
                //    records: "data.totalCount"
                //};

                $grid.jqGrid('setGridParam', {
                    postData: params
                }).trigger('reloadGrid', [
                    {
                        page: 1
                    }
                ]);

            },

            getParam: function () {
                var self = this;
                var $container = self.options.$container;

                var $grid = self.$grid;
                var params = {};
                var $form = $container.find('form[name="basicFrm"]');

                var $elements = $.makeArray($form.prop('elements')).filter(function () {
                    return true; //this.name;
                });

                var name;
                var key;
                $.each($elements, function () {
                    name = this.name;
                    if (name) {

                        if (this.type == 'radio') {
                            params[name] = $('input[name="' + name + '"]:checked').val() || '';
                        } else
                            params[name] = $.trim($(this).val()) || '';

                    }

                });

                return params;

            },

            bindEvent: function () {
                var self = this;
                self.bindBasicEvent();
            },

            bindBasicEvent: function () {
                var self = this;
                var $container = self.options.$container;
                var $form = $container.find('form[name="basicFrm"]');
                self.bindFormEvent($form);
                $container.find('.query-btn').on('click.query', function () {
                    self.query();
                    return false;
                });

                $form.find('button[type="reset"]').click(function() {
                    self.resetQueryForm();
                });

                $form.find('input[name=streamName]').keydown(function(e){
                    if(e.keyCode==13){
                        self.query();
                    }
                })
            },

            bindFormEvent: function ($form) {
            }

        })
    }
)
